<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_avatar">
        <img src="../assets/img/logo.png" alt="" class="login_logo" />
      </div>
      <el-form
        ref="loginformRef"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="login_btns">
          <el-button type="primary" @click="handelLogin">登录</el-button>
          <el-button type="info" @click="handelReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { GoLogin } from '../request/api.js'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在3到 6个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在6到16个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {},
  methods: {
    //  点击重置按钮的时候  重置表单
    handelReset() {
      this.$refs.loginformRef.resetFields()
    },
    handelLogin() {
      // valid 是验证的结果  strobj是未通过验证的字段对象
      this.$refs.loginformRef.validate((valid, strobj) => {
        if (!valid) {
          return false
        }
        GoLogin({
          username: this.loginForm.username,
          password: this.loginForm.password
        }).then(res => {
          const { data: Alldata } = res
          const { data, meta } = Alldata
          if (meta.status !== 200) {
            this.$message.error(meta.msg)
            return false
          }
          window.sessionStorage.setItem('token', data.token)
          this.$message.success(meta.msg)
          this.$router.push('/home')
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background: #2b4b6b;
  height: 100%;
  position: relative;
  .login_box {
    width: 400px;
    height: 300px;
    position: absolute;
    background: #fff;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .login_avatar {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      margin: 0 auto;
      margin-top: -60px;
      background: #fff;
      overflow: hidden;
      padding: 10px;
      box-sizing: border-box;
      .login_logo {
        width: 100%;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      padding: 0 20px;
      width: 100%;
      box-sizing: border-box;
      .login_btns {
        box-sizing: border-box;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
